package com.example.goodrubish;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.goodrubish.Fragment.SearchFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Detail extends AppCompatActivity {

    SharedPreferences sp,sp1;
    private TextView garbage_name;
    private TextView garbage_type;
    private TextView garbage_explain;
    private TextView garbage_contain;
    private TextView garbage_tips;
    private ImageView garbage_picture;
    private Button button;

    private JSONObject search_result= SearchFragment.Search_JSON;
    private int detail_garbage_id= Search.garbage_id;
    private String[] garbage_type_string={"可回收垃圾","有害垃圾","厨余垃圾","其它垃圾"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ActionBar actionbar=getSupportActionBar();
        if(actionbar!=null){
            actionbar.hide();
        }
        garbage_name=findViewById(R.id.detail_garbage_name);
        garbage_type=findViewById(R.id.detail_garbage_type);
        garbage_explain=findViewById(R.id.detail_garbage_explain);
        garbage_contain=findViewById(R.id.detail_garbage_contain);
        garbage_tips=findViewById(R.id.detail_garbage_tips);
        garbage_picture=findViewById(R.id.detail_garbage_picture);
        button = findViewById(R.id.button);
        sp1 = getSharedPreferences("config",Context.MODE_PRIVATE);
        String name1 = sp1.getString("name",null);
        sp = getSharedPreferences(name1, Context.MODE_PRIVATE);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String name = " ";
                    String path;
                    name = make_the_String("name");
                    if(name!=" "){
                        path = "https://baike.baidu.com/item/" + name;
                    }else{
                        path = "https://baike.baidu.com/";
                    }
                    Uri uri = Uri.parse(path);
                    Intent intent=new Intent(Intent.ACTION_VIEW,uri);
                    startActivity(intent);
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
        });

        try {
            String name = make_the_String("name");
            garbage_name.setText(name);
            List<String> list = new ArrayList<String>();
            String tjson = sp.getString("key_history",null);
            if(tjson!=null){
                Gson gson = new Gson();
                list = gson.fromJson(tjson,  new TypeToken<List<String>>(){}.getType());
            }
            list.add(name);
            Gson gson = new Gson();
            String jsonstr = gson.toJson(list);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("key_history",jsonstr);
            editor.commit();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            String s = garbage_type_string[Integer.parseInt(make_the_String("type"))];
            switch (s){
                case "可回收垃圾":
                    garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.recyclablewast));
                    break;
                case "有害垃圾":
                    garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.harmfulwaste));
                    break;
                case "厨余垃圾":
                    garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.kitchenwaste));
                    break;
                case "其它垃圾":
                    garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.otherwaste));
                    break;
            }
            garbage_type.setText(garbage_type_string[Integer.parseInt(make_the_String("type"))]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            garbage_explain.setText("    "+make_the_String("explain"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            garbage_contain.setText("    "+make_the_String("contain"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            garbage_tips.setText("    小贴士："+make_the_String("tip"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String make_the_String(String need) throws JSONException {            //用于将JSON转换为字符串数组
        JSONArray newslist=search_result.getJSONArray("newslist");
//        String[] name=new String[newslist.length()];
        String newslistString=new String();
        JSONObject the_garbage_JSON=new JSONObject();       //具体某一项的json ，可以通过getstring获得其中具体的项
        newslistString=newslist.getString(detail_garbage_id);
        the_garbage_JSON=new JSONObject(newslistString);
//        name[i]=the_garbage_JSON.getString("name");
        return the_garbage_JSON.getString(need);
    }
}